    //
    //  LoginView.m
    //  Kids
    //
    //  Created by LiTengFei on 14/11/26.
    //  Copyright (c) 2014年 90's Technology Co.Ltd. All rights reserved.
    //

#import "LoginView.h"
#import "SVProgressHUD.h"
#import "ChangePasswordViewController.h"
#import "AccountInfo.h"
@interface LoginView () <UITextFieldDelegate>
@property (nonatomic, strong) UITextField* phoneTextField;
@property (nonatomic, strong) UITextField* passwordTextField;

@end
@implementation LoginView

- (instancetype)init
{
    if (self = [super init]) {
        [self commonInstance];
    }
    return self;
}
-(void)setOriginRect:(CGRect)originRect{
    _originRect =originRect;
    self.frame  = originRect;
}
-(void)commonInstance{
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"signin_frame"]];
    
    [self addSubview:imageView];
    
    self.frame = CGRectMake(21, 360, 278, 188);
    
    UIImageView* phoneBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gray1"]];
    phoneBackground.frame = CGRectMake(15, 15, 248, 40);
    [self addSubview:phoneBackground];
    
    UIImageView* passwordBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gray1"]];
    passwordBackground.frame = CGRectMake(15, 65, 248, 40);
    [self addSubview:passwordBackground];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(25, 15, 228, 40)];
    self.phoneTextField.placeholder = @"手机号码";
    self.phoneTextField.delegate = self;
    self.phoneTextField.returnKeyType = UIReturnKeyDone;
    [self addSubview:self.phoneTextField];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(25, 65, 228, 40)];
    [self.passwordTextField setSecureTextEntry:YES];
    self.passwordTextField.placeholder = @"密码";
    self.passwordTextField.delegate = self;
    self.passwordTextField.returnKeyType = UIReturnKeyDone;
    [self addSubview:self.passwordTextField];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 38);
    button.center = CGPointMake(CGRectGetWidth(self.frame) / 2, 135);
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"yellow_press"] forState:UIControlStateHighlighted];
    
    [button addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    
    UIButton* forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPassword.frame = CGRectMake(CGRectGetWidth(self.frame) / 2 + 50, 135, 80, 20);
    [forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPassword setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    forgetPassword.titleLabel.font = [UIFont systemFontOfSize:14.0];
    
    [forgetPassword addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:forgetPassword];
    
    UIImageView* tipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"words"]];
    
    tipImageView.frame = CGRectMake(0, 0, 168, 20);
    tipImageView.center = CGPointMake(CGRectGetWidth(self.frame) / 2, 170);
    
    [self addSubview:tipImageView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    
    self.account = [[AccountInfo shareInstance] phone];
    self.password = [[AccountInfo shareInstance] password];
}
-(void)forgetPassword:(UIButton *)button{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(forgetPasswordClick)]){
        [self.delegate forgetPasswordClick];
    }
    
}
- (void)loginClick:(UIButton*)button
{
    
    if (self.phoneTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
        //验证
    NSParameterAssert(self.phoneTextField.text.length > 0);
    NSParameterAssert(self.passwordTextField.text.length > 0);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginClick:phone:password:)]) {
        [self.delegate loginClick:button phone:self.phoneTextField.text password:self.passwordTextField.text];
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardShow:(NSNotification*)notification
{
    self.frame = CGRectMake(21, self.originRect.origin.y - 180, 278, 188);
}
- (void)keyboardHide:(NSNotification*)notification
{
    self.frame = self.originRect;
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)setAccount:(NSString *)account{
    _account =account;
    self.phoneTextField.text = account;
}
-(void)setPassword:(NSString *)password{
    _password = password;
    self.passwordTextField.text = password;
}
@end
